package org.alex.nettychat.netty.custom.console;

import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author : alex
 */
public class ConsoleCommandManager implements ConsoleCommand {

    private Map<String, ConsoleCommand> consoleCommandMap;

    public ConsoleCommandManager() {
        consoleCommandMap = new HashMap<>();
        consoleCommandMap.put("SendToUser", new SendToUser());
        consoleCommandMap.put("CreateGroup", new CreateGroup());
        consoleCommandMap.put("Logout", new Logout());
        consoleCommandMap.put("JoinGroup", new JoinGroup());
        consoleCommandMap.put("QuitGroup", new QuitGroup());
        consoleCommandMap.put("GetGroupInfo", new GetGroupInfo());
        consoleCommandMap.put("SendToGroup", new SendToGroup());
    }

    @Override
    public void exec(Scanner scanner, Channel channel) {
        // 获取第一个指令
        System.out.println("请输入指令:");
        String command = scanner.nextLine();
        ConsoleCommand consoleCommand = consoleCommandMap.get(command);
        if (consoleCommand != null) {
            consoleCommand.exec(scanner, channel);
        } else {
            System.err.println("无法识别[" + command + "]指令, 请重新输入");
        }
    }
}
